package org.wattdepot.cli.command;

import java.util.List;
import org.wattdepot.cli.CommandLineInterface;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.summary.jaxb.SourceSummary;

/**
 * Prints information about a single source.
 * 
 * @author Anthony Xu & John Mack
 * 
 */
public class ListSourceSummaryCommand implements WattDepotCliCommand {

  /**
   * Prints information about a single source.
   * 
   * @param cli WattDepotClient with hostUri = http://server.wattdepot.org:8182/wattdepot/
   * @param commands user's command line input
   * @return string to output
   */
  public String doCommand(CommandLineInterface cli, List<String> commands) {
    String outputString = "";
    StringBuffer buffer = new StringBuffer();
    try {
      Source sourceClient = cli.getClient().getSource(commands.get(1));
      if (sourceClient.getSubSources() == null) {
        outputString += "Source: " + commands.get(1) + "\n";
        outputString += "SubSources: None" + "\n";

      }
      else {
        outputString += "Source: " + commands.get(1) + "\n";
        for (String subsource : sourceClient.getSubSources().getHref()) {
          buffer.append(subsource.substring(subsource.indexOf("SIM")));
          buffer.append(',');
        }
        outputString += "SubSources: " + buffer.toString() + "\n";

      }
      outputString += "Description: " + sourceClient.getDescription() + "\n";
      outputString += "Owner: " + sourceClient.getOwner() + "\n";
      outputString += "Location: " + sourceClient.getOwner() + "\n";
      outputString += "Coordinates: " + sourceClient.getCoordinates() + "\n";
      outputString += "Properties: " + sourceClient.getProperties() + "\n";
      /**
      for (Property property : sourceClient.getProperties().getProperty()) {
        outputString += "(" + property.getKey() + " : " + property.getValue() + "), ";       
      }
      */
      SourceSummary summary = null;
      summary = cli.getClient().getSourceSummary(commands.get(1));
      outputString += "Earliest Data: " + summary.getFirstSensorData() + "\n";
      outputString += "Latest Data: " + summary.getLastSensorData() + "\n";
      outputString += "Total Data Points: " + summary.getTotalSensorDatas() + "\n";
    }
    catch (NullPointerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (NotAuthorizedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (ResourceNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (BadXmlException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return outputString;
  }

}
